<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Otent extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('otent_model');
	}
	
	function index(){
		if(!$this->session->userdata('username')){
		$this->load->view('login');
		} else{
			redirect('kuesioner/list_kuesioner');
			}
	}
	
	function auth(){
		$username = $this->input->post('username');
		$pass = $this->input->post('password');
		
		if($username==null || $pass==null){
			echo '<script>alert("anda belum login!")</script>';
			redirect('guest');
			
		} else {
			$user = $this->otent_model->get_user($username, md5($pass));
			if($user != null){
			if($user['status'] == 1)
			{
				if($user['role'] == 1)
				{
					$newuser = array(
						'username' => $user['nama'],
						'id_user' => $user['id_user'],
						'role' => $user['role']
					);
				
                $this->session->set_userdata($newuser);
				
				$alerts[] = array('success', 'Anda login sebagai '.$this->session->userdata('username').'!');
				$this->session->set_flashdata('alerts', $alerts);
			    redirect('guest/list_data');
				} else
					{
						$newuser = array(
						'username' => $user['nama'],
						'id_user' => $user['id_user'],
						'role' => $user['role']
						);
					
					$this->session->set_userdata($newuser);
					
					redirect('admin/list_user');
					}
			} else
				{
					$alerts[] = array('error', 'Akun anda masih belum aktif!');
					$this->session->set_flashdata('alerts', $alerts);
					redirect('guest');
				}
				
            } else{
				$alerts[] = array('error', 'Akun yang anda masukkan salah!');
				$this->session->set_flashdata('alerts', $alerts);
				redirect('guest');
			} 
		}
		
	}
		function out() {//logout dipasang di main, karena logout yang ada di main yang digunakan di semua aplikasi
        $this->session->sess_destroy();
        redirect('guest');
    }



}
?>